package com.ruoyi.biz.service.impl;

import java.util.List;

import com.ruoyi.biz.domain.BizAddress;
import com.ruoyi.biz.mapper.BizAddressMapper;
import com.ruoyi.biz.service.IBizAddressService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 收货地址Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-16
 */
@Service
public class BizAddressServiceImpl implements IBizAddressService {
    @Autowired
    private BizAddressMapper bizAddressMapper;

    /**
     * 查询收货地址
     *
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public BizAddress selectBizAddressById(Long id) {
        return bizAddressMapper.selectBizAddressById(id);
    }

    /**
     * 查询收货地址列表
     *
     * @param bizAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<BizAddress> selectBizAddressList(BizAddress bizAddress) {
        return bizAddressMapper.selectBizAddressList(bizAddress);
    }

    /**
     * 新增收货地址
     *
     * @param bizAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertBizAddress(BizAddress bizAddress) {
        if (checkMaxCount(bizAddress)) {
            throw new ServiceException(MessageUtils.message("user.address.max.check"), HttpStatus.ERROR);
        }
        bizAddress.setCreateTime(DateUtils.getNowDate());
        return bizAddressMapper.insertBizAddress(bizAddress);
    }

    private boolean checkMaxCount(BizAddress bizAddress) {
        BizAddress condition = new BizAddress();
        condition.setUserId(bizAddress.getUserId());
        List<BizAddress> addresses = bizAddressMapper.selectBizAddressList(condition);
        if (addresses.size() >= 9) {
            return true;
        }
        return false;
    }


    /**
     * 修改收货地址
     *
     * @param bizAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateBizAddress(BizAddress bizAddress) {
        bizAddress.setUpdateTime(DateUtils.getNowDate());
        return bizAddressMapper.updateBizAddress(bizAddress);
    }

    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteBizAddressByIds(Long[] ids) {
        return bizAddressMapper.deleteBizAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteBizAddressById(Long id) {
        return bizAddressMapper.deleteBizAddressById(id);
    }

    /**
     * 查询默认收货地址
     *
     * @param userId 用户ID
     * @return 收货地址
     */
    @Override
    public BizAddress selectDefaultAddress(long userId) {
        return bizAddressMapper.selectDefaultAddress(userId);
    }

    @Override
    public boolean updateAddressDefault(Long id) {
        BizAddress address = selectBizAddressById(id);
        long userId = address.getUserId();
        BizAddress condition = new BizAddress();
        condition.setUserId(userId);
        condition.setDefaultFlag(1);
        List<BizAddress> list = selectBizAddressList(condition);
        if (!CollectionUtils.isEmpty(list)) {
            BizAddress bizAddress = list.get(0);
            BizAddress updateAddress = new BizAddress();
            updateAddress.setId(bizAddress.getId());
            bizAddress.setDefaultFlag(0);
            bizAddressMapper.updateBizAddress(bizAddress);
        }
        address.setDefaultFlag(1);
        bizAddressMapper.updateBizAddress(address);
        return true;
    }
}
